﻿// ===================================
// The use and distribution terms for this software are covered by the Microsoft public license, 
// visit for more info : http://qtest.codeplex.com 
// 
// You must not remove this copyright notice, or any other, from this software
// 
// Senthil Maruthaiappan  senips@gmail.com
// ===================================

using System;
using System.Collections;
using System.Text;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using QAPro.Test.FitServices;
using QAPro.Test.FitServices.Interfaces;
using QAPro.Test.Interfaces;
using QAPro.Test.Library;

namespace UnitTests.QAPro.Test.Library
{
    [TestClass]
    public class TestFitUtilService
    {
        private readonly IFitUtilService _fitUtilSvc = new FitUtilService();
        private readonly TestServiceContext _testServiceContext = new TestServiceContext();

        [TestMethod]
        public void TestgetColumns()
        {
            String[] result = _fitUtilSvc.GetColumns("!|eg.Division|");

            Assert.AreEqual(1, result.GetLength(0));

            result = _fitUtilSvc.GetColumns("|numerator|denominator|quotient?|");
            Assert.AreEqual(3, result.GetLength(0));


            result = _fitUtilSvc.GetColumns("");
            Assert.AreEqual(0, result.GetLength(0));

            result = _fitUtilSvc.GetColumns("|10       |2          |5        |32|");
            Assert.AreEqual(4, result.GetLength(0));
        }


        [TestMethod]
        public void TestgetAsHtml()
        {
            String[] result = _fitUtilSvc.GetColumns("!|eg.Division|");

            Assert.AreEqual(1, result.GetLength(0));
            IFixtureData fixdata = new FixtureData("");
            fixdata.Add(result);
            var sb = new StringBuilder();
            _fitUtilSvc.GetAsHtmlTable(fixdata, ref sb);

            result = _fitUtilSvc.GetColumns("|numerator|denominator|quotient?|");
            Assert.AreEqual(
                "<table border=\"1\" cellspacing=\"0\"><tbody><tr><td colspan=\"1\">eg.Division</td></tr></tbody></table>",
                sb.ToString().Trim());
        }

        [TestMethod]
        public void TestGetColumnTag()
        {
            Assert.AreEqual("<td>sample</td>", _fitUtilSvc.GetColumnTag("sample", 0));
            Assert.AreEqual("<td colspan=\"4\">sample</td>", _fitUtilSvc.GetColumnTag("sample", 4));
        }

        [TestMethod]
        public void TestGetRowTag()
        {
            Assert.AreEqual("<tr>sample</tr>", _fitUtilSvc.GetRowTag("sample"));
        }

        [TestMethod]
        public void TestGetTableEndTag()
        {
            Assert.AreEqual("</tbody></table>", _fitUtilSvc.GetTableEndTag());
        }

        [TestMethod]
        public void TestGetTableStartTag()
        {
            Assert.AreEqual("<table border=\"1\" cellspacing=\"0\"><tbody>", _fitUtilSvc.GetTableStartTag());
        }


        [TestMethod]
        public void TestFixPath()
        {
            Assert.AreEqual("Suite\\", _fitUtilSvc.FixPath(@"Suite"));
            Assert.AreEqual("Suite\\", _fitUtilSvc.FixPath(@"Suite\"));
        }

        [TestMethod]
        public void TestIsTestDir()
        {
            Assert.IsFalse(_fitUtilSvc.IsTestDir(@"TestDataCollection01\FitnesseRoot\GrandSuite1"));
            Assert.IsFalse(_fitUtilSvc.IsTestDir(@"TestDataCollection01\FitnesseRoot\GrandSuite1\CommonData"));
            Assert.IsFalse(_fitUtilSvc.IsTestDir(@"TestDataCollection01\FitnesseRoot\GrandSuite1\CommonData\Initialize"));
            Assert.IsTrue(
                _fitUtilSvc.IsTestDir(@"TestDataCollection01\FitnesseRoot\GrandSuite1\Suite1\Suite11\TestCase11"));
        }

        [TestMethod]
        public void TestIsRunnable()
        {
            Assert.IsTrue(_fitUtilSvc.IsRunnable(@"TestDataCollection01\FitnesseRoot\GrandSuite1"));
            Assert.IsFalse(_fitUtilSvc.IsRunnable(@"TestDataCollection01\FitnesseRoot\GrandSuite1\CommonData"));
            Assert.IsFalse(_fitUtilSvc.IsRunnable(@"TestDataCollection01\FitnesseRoot\GrandSuite1\CommonData\Initialize"));
            Assert.IsTrue(
                _fitUtilSvc.IsRunnable(@"TestDataCollection01\FitnesseRoot\GrandSuite1\Suite1\Suite11\TestCase11"));
        }

        [TestMethod]
        public void TestIsFitnesseTableBeginTag()
        {
            Assert.IsTrue(_fitUtilSvc.IsFitnesseTableBeginTag("!|myfixture.eg|"));
            Assert.IsFalse(_fitUtilSvc.IsFitnesseTableBeginTag("|myfixture.eg|"));
            Assert.IsFalse(_fitUtilSvc.IsFitnesseTableBeginTag("|!myfixture.eg|"));
        }

        [TestMethod]
        public void TestCleanFixtureTableTitle()
        {
            Assert.AreEqual("myfixture.eg", _fitUtilSvc.FormatFixtureTableTitle("!|myfixture.eg|"));
            Assert.AreEqual("myfixture.eg <param1>", _fitUtilSvc.FormatFixtureTableTitle("!|myfixture.eg|param1|"));
            Assert.AreEqual("myfixture.eg <param1> <param2>",
                            _fitUtilSvc.FormatFixtureTableTitle("!|myfixture.eg|param1|param2|"));
        }

        [TestMethod]
        public void TestFixtureRowList()
        {
            IFixtureData list = new FixtureData("My Fixture");

            list.Add(_fitUtilSvc.GetColumns("!|eg.Division|"));

            Assert.AreEqual(1, list[0].GetLength(0));

            list.Add(_fitUtilSvc.GetColumns("|numerator|denominator|quotient?|value?|"));
            Assert.AreEqual(4, list[1].GetLength(0));

            list.Add(_fitUtilSvc.GetColumns("|numerator|quotient?|"));
            Assert.AreEqual(2, list[2].GetLength(0));


            Assert.AreEqual(4, list.MaxColumn);
        }


        [TestMethod]
        public void TestParentPath()
        {
            var rootSuite = _testServiceContext.Services.Get<ISuite>("Root");
            ISuite suite = rootSuite.CreateSuite("Test Suite");
            ITestCase test = suite.CreateTestCase("New Test Case");
            IFixtureTable ftable = test.CreateFixtureTable("Sample Fixture Table-1");
            string parentPath = string.Empty;
            TreeChildEnumerator.FindParentPath(test, ref parentPath);
            Assert.AreEqual(@"Root\Test Suite\", parentPath);
        }


        [TestMethod]
        public void TestResolveIncludes()
        {
            var rootSuite = _testServiceContext.Services.Get<ISuite>("Root");
            ISuite suite = rootSuite.CreateSuite("Suite1");
            suite.IsRunnable = true;
            ITestCase test = suite.CreateTestCase("TestCase1");
            test.IsRunnable = true;

            test.CreateFixtureTable("Sample Fixture Table-1");
            var aIncludeScript = _testServiceContext.Services.Get<ITestLink>("includetable1");
            aIncludeScript.ContentText = @"Root\commondata";
            test.AddChildNode(aIncludeScript);
            test.CreateFixtureTable("Sample Fixture Table-2");


            ISuite commdataSuite = rootSuite.CreateSuite("commondata");
            commdataSuite.IsRunnable = false;
            commdataSuite.AddChildNode(_testServiceContext.Services.Get<IFixtureTable>("table1"));
            commdataSuite.AddChildNode(_testServiceContext.Services.Get<IFixtureTable>("table2"));

            var lookupTable = new Hashtable
                                  {
                                      {TreeChildEnumerator.GetFullPath(rootSuite).ToUpper(), rootSuite},
                                      {TreeChildEnumerator.GetFullPath(suite).ToUpper(), suite},
                                      {TreeChildEnumerator.GetFullPath(test).ToUpper(), test},
                                      {TreeChildEnumerator.GetFullPath(commdataSuite).ToUpper(), commdataSuite}
                                  };
            _fitUtilSvc.ResolveIncludes(rootSuite, lookupTable, null);
            Assert.AreEqual(1, aIncludeScript.Count);
            Assert.AreEqual(2, aIncludeScript[0].Count);
            Assert.AreEqual(commdataSuite, aIncludeScript[0]);


            var aIncludeScript2 = _testServiceContext.Services.Get<ITestLink>("includetable1");
            aIncludeScript2.ContentText = @"Root\commondata";
            test.AddChildNode(aIncludeScript2);

            commdataSuite.CreateTestCase("testcase");
            _fitUtilSvc.ResolveIncludes(rootSuite, lookupTable, null);
            Assert.AreEqual(0, aIncludeScript2.Count);
        }


        [TestMethod]
        public void TestArgumentPath()
        {
            Assert.IsTrue(_fitUtilSvc.IsArgumentsValid("TestDataCollection01\\FitnesseRoot\\GrandSuite1"));
            Assert.IsTrue(_fitUtilSvc.IsArgumentsValid("TestDataCollection01\\FitnesseRoot\\GrandSuite1"));
        }

        [TestMethod]
        public void TestGetFitnesseRootPath()
        {
            const string testPath = "TestDataCollection01\\FitnesseRoot\\GrandSuite1";
            const string exptectedpath = "TestDataCollection01\\FitnesseRoot\\";
            Assert.IsTrue(_fitUtilSvc.GetFitnesseRootPath(testPath).EndsWith(exptectedpath.ToLower()));
        }

        [TestMethod]
        public void TestDoWikiToHtml()
        {
            Assert.IsTrue(_fitUtilSvc.DoWikiToHtml("!1 sample").StartsWith("<h1> sample</h1>"));
            Assert.IsTrue(_fitUtilSvc.DoWikiToHtml("!2 sample").StartsWith("<h2> sample</h2>"));
            Assert.IsTrue(_fitUtilSvc.DoWikiToHtml("!3 sample").StartsWith("<h3> sample</h3>"));

            Assert.AreEqual(0, _fitUtilSvc.DoWikiToHtml("!path eg.dll").Length);
        }


        [TestMethod]
        public void TestFlattenTheNodes()
        {
            var rootSuite = _testServiceContext.Services.Get<ISuite>("Root");
            ISuite suite = rootSuite.CreateSuite("Suite1");
            suite.IsRunnable = true;
            ITestCase test = suite.CreateTestCase("TestCase1");
            test.IsRunnable = true;

            test.CreateFixtureTable("Sample Fixture Table-1");
            test.CreateFixtureTable("Sample Fixture Table-2");


            ISuite commdataSuite = rootSuite.CreateSuite("commondata");
            commdataSuite.AddChildNode(_testServiceContext.Services.Get<IFixtureTable>("table1"));
            commdataSuite.AddChildNode(_testServiceContext.Services.Get<IFixtureTable>("table2"));
            commdataSuite.AddChildNode(_testServiceContext.Services.Get<IFixtureTextData>("table3"));


            rootSuite.GetTreeChildEnumerator().ApplyLevelNumbers();


            Assert.AreEqual(0, rootSuite.LevelNo);
            Assert.AreEqual(1, suite.LevelNo);
            Assert.AreEqual(2, test.LevelNo);
            Assert.AreEqual(1, commdataSuite.LevelNo);


            Assert.AreEqual(8, rootSuite.GetTreeChildEnumerator().ToList().Count);
        }
    }
}